//
//  RocBluetoothView.m
//  RocRemoteControlProject
//
//  Created by 新致李照鹏 on 2018/12/26.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "RocBluetoothView.h"

@interface RocBluetoothView ()

// 状态
@property (nonatomic, strong) UIImageView *stateImageView;

// labels
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UILabel *label3;
@property (nonatomic, strong) UILabel *label4;

// 提示和按钮
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIButton *button;



@end

@implementation RocBluetoothView

#pragma mark - 类方法
/**
 展示什么样的界面
 */
- (void)showWithState:(RocBluetoothViewState)state
{
    self.label1.hidden = NO;
    self.label2.hidden = NO;
    self.label3.hidden = NO;
    self.label4.hidden = NO;
    self.tipLabel.hidden = NO;
    self.button.hidden = NO;
    
    switch (state) {
        case RocBluetoothViewStateNotOpen:
        {
            [self todo_OpenBluetooth];
        }
            break;
        case RocBluetoothViewStateOpen:
        {
            [self todo_StartScan];
        }
            break;
        case RocBluetoothViewStateScaning:
        {
            [self todo_Scaning];
        }
            break;
        case RocBluetoothViewStateNotFind:
        {
            [self todo_NotFindDevice];
        }
            break;
        case RocBluetoothViewStateGet:
        {
            [self todo_GetDevice];
        }
            break;
        case RocBluetoothViewStateBind:
        {
            [self todo_BindingDevice];
        }
            break;
            
        default:
            break;
    }
    self.currentState = state;
}
/**
 传入设备名
 */
- (void)setupDeviveName:(NSString *)name
{
    self.label4.text = name;
}
#pragma Mark - 业务逻辑
// 蓝牙未开启
- (void)todo_OpenBluetooth
{
    //  network1.png 开启设备 确认设备处于开机状态 确认手机蓝牙已经开启 手机蓝牙未开启 点击开启蓝牙
    self.stateImageView.image = [UIImage imageNamed:@"network1.png"];
    self.label1.hidden = YES;
    self.label2.text = @"开启设备";
    self.label3.text = @"确认设备处于开机状态";
    self.label4.text = @"确认手机蓝牙已经开启";
    self.tipLabel.text = @"手机蓝牙未开启";
    [self.button setTitle:@"点击开启蓝牙" forState:UIControlStateNormal];
}
// 扫描设备
- (void)todo_StartScan
{
    //  network1.png 开启设备 确认设备处于开机状态 确认手机蓝牙已经开启  开始搜索设备
    self.stateImageView.image = [UIImage imageNamed:@"network1.png"];
    self.label1.hidden = YES;
    self.label2.text = @"开启设备";
    self.label3.text = @"确认设备处于开机状态";
    self.label4.text = @"确认手机蓝牙已经开启";
    self.tipLabel.hidden = YES;
    [self.button setTitle:@"开始搜索设备" forState:UIControlStateNormal];
}
// 扫描设备中...
- (void)todo_Scaning
{
    //  network2.png 正在玩命搜索设备，请等待……
    self.stateImageView.image = [UIImage imageNamed:@"network2.png"];
    self.label1.text = @"正在玩命搜索设备，请等待……";
    self.label2.hidden = YES;
    self.label3.hidden = YES;
    self.label4.hidden = YES;
    self.tipLabel.hidden = YES;
    self.button.hidden = YES;
}

// 没搜索到设备
- (void)todo_NotFindDevice
{
    //  network3.png 搜索失败 未搜索到设备  请确认设备已开启  重新搜索
    self.stateImageView.image = [UIImage imageNamed:@"network3.png"];
    self.label1.hidden = YES;
    self.label2.text = @"搜索失败";
    self.label3.text = @"未搜索到设备";
    self.label4.text = @"请确认设备已开启";
    self.tipLabel.hidden = YES;
    [self.button setTitle:@"重新搜索" forState:UIControlStateNormal];
}

// 成功搜索到设备
- (void)todo_GetDevice
{
    //  network4.png 搜索成功 搜索到设备  请确认设备已开启  重新搜索
    self.stateImageView.image = [UIImage imageNamed:@"network4.png"];
    self.label1.hidden = YES;
    self.label2.text = @"搜索成功";
    self.label3.text = @"搜索到设备，请绑定。";
    self.label4.text = @"设备名称";
    self.tipLabel.hidden = YES;
    [self.button setTitle:@"立即绑定" forState:UIControlStateNormal];
}

// 绑定设备
- (void)todo_BindingDevice
{
    //  network4.png 搜索成功 搜索到设备  请确认设备已开启  重新搜索
    self.stateImageView.image = [UIImage imageNamed:@"network5.png"];
    self.label1.text = @"绑定成功，开启魔法之旅吧！";
    self.label2.hidden = YES;
    self.label3.hidden = YES;
    self.label4.hidden = YES;
    self.tipLabel.hidden = YES;
    self.button.hidden = YES;
}

#pragma mark - 初始化
/**
  初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self buildStateImageView];
        
        [self buildLabel1];
        [self buildLabel2];
        [self buildLabel3];
        [self buildLabel4];
        [self buildLabel5];
        
        [self buildButton];
        
    }
    return self;
}

// 当前状态图
- (void)buildStateImageView
{
    CGFloat width = KROCConver_750(360);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(LK_SCREEN_MIN_LENGTH/2-width/2, KROCConver_750(84), width, width)];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"network1.png"];
    [self addSubview:imageView];
    
    self.stateImageView = imageView;
}

// 第一行文本
- (void)buildLabel1
{
    CGRect rect = CGRectMake(0, CGRectGetMaxY(self.stateImageView.frame)+KROCConver_750(40), LK_SCREEN_MIN_LENGTH, KROCConver_750(50));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:KROCConver_750(26)];
    label.textColor = [UIColor colorFromHexString:@"#787878"];
    label.text = @"正在玩命搜索设备，请等待...";
    [self addSubview:label];
    
    self.label1 = label;
}

// 第二行文本
- (void)buildLabel2
{
    CGRect rect = CGRectMake(0, CGRectGetMaxY(self.label1.frame), LK_SCREEN_MIN_LENGTH, KROCConver_750(100));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:KROCConver_750(32)];
    label.textColor = [UIColor colorFromHexString:@"#4b4b4b"];
    label.text = @"开启设备";
    [self addSubview:label];
    
    self.label2 = label;
}

// 第三行文本
- (void)buildLabel3
{
    CGRect rect = CGRectMake(0, CGRectGetMaxY(self.label2.frame), LK_SCREEN_MIN_LENGTH, KROCConver_750(50));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:KROCConver_750(26)];
    label.textColor = [UIColor colorFromHexString:@"#787878"];
    label.text = @"确认设备处于开机状态";
    [self addSubview:label];
    
    self.label3 = label;
}

// 第四行文本
- (void)buildLabel4
{
    CGRect rect = CGRectMake(0, CGRectGetMaxY(self.label3.frame), LK_SCREEN_MIN_LENGTH, KROCConver_750(50));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:KROCConver_750(26)];
    label.textColor = [UIColor colorFromHexString:@"#787878"];
    label.text = @"确认手机蓝牙已经开启";
    [self addSubview:label];
    
    self.label4 = label;
}

// 提示文本
- (void)buildLabel5
{
    CGRect rect = CGRectMake(0, LK_SCREEN_MAX_LENGTH-KROCConver_750(320), LK_SCREEN_MIN_LENGTH, KROCConver_750(50));
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:KROCConver_750(26)];
    label.textColor = [UIColor colorFromHexString:@"#0ae38f"];
    label.text = @"手机蓝牙未开启";
    [self addSubview:label];
    
    self.tipLabel = label;
}

//
- (void)buildButton
{
    CGRect rect = CGRectMake(KROCConver_750(100), LK_SCREEN_MAX_LENGTH-KROCConver_750(230), LK_SCREEN_MIN_LENGTH-KROCConver_750(100)*2, KROCConver_750(80));
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    button.backgroundColor = [UIColor colorFromHexString:@"#0ae38f"];
    [button setTitle:@"立即绑定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:KROCConver_750(32)];
    button.titleLabel.textColor = [UIColor colorFromHexString:@"#ffffff"];
    button.clipsToBounds = YES;
    button.layer.cornerRadius = KROCConver_750(80)/2;
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button = button;
}

- (void)buttonAction:(UIButton *)sender
{
    RocBluetoothViewButtonType type;
    if (self.currentState == RocBluetoothViewStateNotOpen) {
        type = RocBluetoothViewButtonOpen;
    }else if (self.currentState == RocBluetoothViewStateOpen){
        type = RocBluetoothViewButtonScan;
    }else if (self.currentState == RocBluetoothViewStateNotFind){
        type = RocBluetoothViewButtonAgain;
    }else{
        type = RocBluetoothViewButtonBinding;
    }
    
    if (self.mydelegate && [self.mydelegate respondsToSelector:@selector(RocBluetoothView:ClickButton:)]) {
        [self.mydelegate RocBluetoothView:self ClickButton:type];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
